<template>
	<view class="tab">
		<scroll-view scroll-x="true" class="tab-scroll">
			<view class="tab-scroll_box">
				<view v-for="(item,index) in list" :key="index" class="tab-scroll_item" 
				@click="clickTab(item,index)" :class="{active:activeIndex===index}">
					{{item.name}}
				</view>
			</view>
		</scroll-view>
	<!-- 	<view class="tab-icons">
			<uni-icons type="gear" size="26" color="#666"></uni-icons>
		</view> -->
	</view>
</template>

<script>
	export default {
		name: "tab",
		props: {
			list:{
				type: Array,
				default(){
					return []
				}
			},
			tabIndex: {
				type: Number,
				default: 0
			},
		},
		// tabIndex的值传给tabIndex
		// watch事件监听data和props中值的变化
		watch:{
			tabIndex(newVal){
				this.activeIndex = newVal
				console.log('当前是',newVal);
			}
		},
		data() {
			return {
				// 选中数组下标
				activeIndex:0
			}
		},
		methods: {
			// 注册事件
		clickTab(item,index){
			this.activeIndex = index
			// 为实现界面和选项的切换
			this.$emit('tab',{
				data:item,
				index:index
			})
		}
		},
	}
</script>

<style lang="scss">
	.tab {
		display: flex;
		width: 100%;
		background-color: #fff;
		box-sizing: border-box;
		// margin-top: 12%;
		align-items: center;
		margin-top: 10px;
		.tab-scroll {
			// 让内容充满整行
			flex: 1;
			overflow: hidden;
			box-sizing: border-box;
			margin: 0 10px;
			// border-bottom: 1px red solid;
			.tab-scroll_box {
				display: flex;
				align-items: center;
				flex-wrap: nowrap;
				height: 45px;
				box-sizing: border-box;

				// background-color: forestgreen;
				.tab-scroll_item {
					// 不挤压内容
					flex-shrink: 0;
					padding: 0 10px;
					color: #333;
					font-size: 14px;
					// 选中高亮
					&.active{
						color:$mk-base-color;
						font-size: 20px;
						font-weight: 500;
					
					}
				}
			}
		}

		.tab-icons {
			// display: flex;
			position: relative;
			justify-content: center;
			align-items: center;
			width: 35px;
			padding: 5px;

			// border: 1px green solid;
			&::after {
				// 后面的小竖线
				content: '';
				position: absolute;
				top: 8px;
				bottom: 8px;
				left: 0;
				width: 2px;
				background-color: #ddd;
			}
		}


	}
</style>
